<template>
	<div class="changeclasstable">
		<div class="navget">
			<router-link to="/coursemanager/coursemanagement/courses" class="nava">课程</router-link>
			<router-link to="/coursemanager/coursemanagement/courseclass" style="color: black;"
				class="nava">班级</router-link>
		</div>
		<el-button type="danger" class="lookclasstable" @click="check" :disabled="checkFlag">检查</el-button>
		<span class="tip">请先检查再修改</span>
		<el-button type="success" class="changeclass" @click="changetable" :disabled="changeFlag">修改</el-button>
		<span @click="backOk" class="back">返回</span>
		<div class="tableFrom">
			<h4>{{ classname }}课程表</h4>
			<el-table :data="tableData" border style="width: 100%">
				<el-table-column prop="level" label="" width="180">
				</el-table-column>
				<el-table-column label="周一">
					<template slot-scope="scope">

						<el-select v-model="scope.row.list[0].subId" placeholder="科目"
							@change="getcourseid(scope.row.list[0])">
							<el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
								:value="item.subId">
							</el-option>
						</el-select>

						<el-select
							:class='{ active: scope.row.list[0].myErrorData.findIndex(v => v == "周1" + scope.row.list[0].timeName) != -1 ? true : false }'
							v-model="scope.row.list[0].teaId" placeholder="任课老师">
							<el-option v-for="item in scope.row.list[0].teacherlist" :key="item.teaId" :label="item.teaName"
								:value="item.teaId">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="周二">
					<template slot-scope="scope">
						<el-select v-model="scope.row.list[1].subId" placeholder="科目"
							@change="getcourseid(scope.row.list[1])">
							<el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
								:value="item.subId">
							</el-option>
						</el-select>

						<el-select
							:class='{ active: scope.row.list[1].myErrorData.findIndex(v => v == "周2" + scope.row.list[1].timeName) != -1 ? true : false }'
							v-model="scope.row.list[1].teaId" placeholder="任课老师">
							<el-option v-for="item in scope.row.list[1].teacherlist" :key="item.teaId" :label="item.teaName"
								:value="item.teaId">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="周三">
					<template slot-scope="scope">
						<el-select v-model="scope.row.list[2].subId" placeholder="科目"
							@change="getcourseid(scope.row.list[2])">
							<el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
								:value="item.subId">
							</el-option>
						</el-select>

						<el-select
							:class='{ active: scope.row.list[2].myErrorData.findIndex(v => v == "周3" + scope.row.list[2].timeName) != -1 ? true : false }'
							v-model="scope.row.list[2].teaId" placeholder="任课老师">
							<el-option v-for="item in scope.row.list[2].teacherlist" :key="item.teaId" :label="item.teaName"
								:value="item.teaId">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="周四">
					<template slot-scope="scope">
						<el-select v-model="scope.row.list[3].subId" placeholder="科目"
							@change="getcourseid(scope.row.list[3])">
							<el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
								:value="item.subId">
							</el-option>
						</el-select>
						<el-select
							:class='{ active: scope.row.list[3].myErrorData.findIndex(v => v == "周4" + scope.row.list[3].timeName) != -1 ? true : false }'
							v-model="scope.row.list[3].teaId" placeholder="任课老师">
							<el-option v-for="item in scope.row.list[3].teacherlist" :key="item.teaId" :label="item.teaName"
								:value="item.teaId">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="周五">
					<template slot-scope="scope">
						<el-select v-model="scope.row.list[4].subId" placeholder="科目"
							@change="getcourseid(scope.row.list[4])">
							<el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
								:value="item.subId">
							</el-option>
						</el-select>
						<el-select
							:class='{ active: scope.row.list[4].myErrorData.findIndex(v => v == "周5" + scope.row.list[4].timeName) != -1 ? true : false }'
							v-model="scope.row.list[4].teaId" placeholder="任课老师">
							<el-option v-for="item in scope.row.list[4].teacherlist" :key="item.teaId" :label="item.teaName"
								:value="item.teaId">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="周六">
					<template slot-scope="scope">
						<el-select v-model="scope.row.list[5].subId" placeholder="科目"
							@change="getcourseid(scope.row.list[5])">
							<el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
								:value="item.subId">
							</el-option>
						</el-select>
						<el-select
							:class='{ active: scope.row.list[5].myErrorData.findIndex(v => v == "周6" + scope.row.list[5].timeName) != -1 ? true : false }'
							v-model="scope.row.list[5].teaId" placeholder="任课老师">
							<el-option v-for="item in scope.row.list[5].teacherlist" :key="item.teaId" :label="item.teaName"
								:value="item.teaId">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="周日">
					<template slot-scope="scope">
						<el-select v-model="scope.row.list[6].subId" placeholder="科目"
							@change="getcourseid(scope.row.list[6])">
							<el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
								:value="item.subId">
							</el-option>
						</el-select>
						<el-select
							:class='{ active: scope.row.list[6].myErrorData.findIndex(v => v == "周日" + scope.row.list[6].timeName) != -1 ? true : false }'
							v-model="scope.row.list[6].teaId" placeholder="任课老师">
							<el-option v-for="item in scope.row.list[6].teacherlist" :key="item.teaId" :label="item.teaName"
								:value="item.teaId">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
			</el-table>
		</div>
	</div>
</template>

<script>
export default {
	data() {
		return {
			tableData: [],
			classname: this.$route.query.classname,
			classlist: [],
			baseUrl: require("../js/baseUrl.js").baseUrl,
			courseslist: [],
			teacherlist: [],
			changeFlag: true,
			checkFlag: true,
			pageIndex: 1,
			pageSize: 20,
			total: "",
			schas: [],
			myErrorData: []
		}
	},
	methods: {
		backOk() {
			this.$router.push('/coursemanager/coursemanagement/courseclass')
		},
		//获取课程表
		getCourses() {
			this.checkFlag = false;
			this.$http.post(this.baseUrl + 'SchoolSys/scha/searchByIClazz', {
				clazzId: Number(this.$route.query.classid),
			}).then(res => {
				// console.log(res);
				if (res.data.code == 200) {
					let resData = res.data.data
					//  console.log(resData)
					let datas = []
					for (var i in resData) {
						// console.log(resData[i])
						let list = [];
						for (var k in resData[i]) {
							list.push({
								courseName: resData[i][k].subName,
								courseTea: resData[i][k].teaName,
								schaId: resData[i][k].schaId,
								teaId: resData[i][k].teaId,
								subId: resData[i][k].subId,
								clazzId: Number(this.$route.query.classid),
								dayName: k,
								weekIndex: k.substring(1, 2),
								teacherlist: [],
								timeName: '',
								myErrorData: []
							})
						}
						//排序list
						for (var m = 0; m < list.length; m++) {
							for (var k = m + 1; k < list.length; k++) {
								if (list[m].weekIndex > list[k].weekIndex) {
									let temp = list[m]
									list[m] = list[k]
									list[k] = temp
								}
							}
						}

						datas.push({
							index: i.substring(1, 2),
							level: i,
							list: list
						})
					}
					// console.log(datas)

					for (var i = 0; i < datas.length; i++) {
						for (var k = i + 1; k < datas.length; k++) {
							if (datas[i].index > datas[k].index) {
								let temp = datas[i]
								datas[i] = datas[k]
								datas[k] = temp
							}
						}
					}

					this.tableData = datas
					console.log(this.tableData)
					this.tableData.map(v => {
						console.log(v.level);
						v.list.map(item => {
							item.timeName = v.level
							this.$http.post(this.baseUrl + "SchoolSys/getTeachersBySubId", {
								subId: item.subId
							}).then(res => {
								//console.log("777",res);
								if (res.data.code == 200) {
									item.teacherlist = res.data.data.teachers
									item.myErrorData = []
								}
							})
						})
					})
				}
			})
		},
		//根据班级id查对应课目，对应老师
		getclasssection() {
			this.$http.post(this.baseUrl + "SchoolSys/clazz/selectclazzsub", {
				clazzId: Number(this.$route.query.classid)
			}).then(res => {
				// console.log("666",res);
				if (res.data.code == 200) {
					this.courseslist = res.data.data.filter(v => v.status == 1)
					//console.log("666",this.courseslist);
				}
			})
		},
		//根据课程id查询老师：
		getcourseid(Courses){
			//console.log("Courses",Courses.id)
			this.$http.post(this.baseUrl + "SchoolSys/getTeachersBySubId", {
				subId:Courses.subId
			}).then(res => {
				//console.log("666",res);
				if(res.data.code==200){
					Courses.teacherlist=res.data.data.teachers
				}
			})
        },
		//检查
		check() {
			//前端验证
			if (this.schas == []) {
				this.$message.success("请完善课表")
			}
			console.log(this.tableData)
			let schas = []
			this.tableData.map(v => {
				// console.log(v.list);
				v.list.map(item => {
					// console.log(item);
					delete item.courseName
					delete item.courseTea
					delete item.weekIndex
					// console.log(item);
					schas.push(item)
				})
			})
			this.schas = schas
			//console.log("schas", this.schas)
			//调用检查接口
			//SchoolSys/scha/check
			this.$http.post(this.baseUrl + "SchoolSys/scha/check", {
				schas: this.schas
			}).then(res => {
				if (res.data.code == 200) {
					this.checkFlag = true
					this.changeFlag = false
					//console.log(this.myErrorData);
					this.myErrorData = []
					this.tableData.forEach(v => {
						v.list.forEach(item => {
							item.myErrorData = this.myErrorData
						})
					})
					//console.log(this.myErrorData);
					this.$message.success("检查完成，请提交")
				} else if (res.data.code == 500) {
					this.$message.error("教师存在上课冲突，请调整")
					this.myErrorData = []
					let errorData = res.data.data
					errorData.forEach(v => {
						this.myErrorData.push(v.dayName + v.timeName)
					})
					//console.log("检查返回", this.myErrorData);
					//console.log('table',this.tableData)
					this.tableData.forEach(v => {
						v.list.forEach(item => {
							item.myErrorData = this.myErrorData
						})
					})

				}
			})
		},
		//修改课程表
		changetable() {
			// this.getSchas();
			this.$http.post(this.baseUrl + "SchoolSys/scha/update", {
				schas: this.schas,
			}).then(res => {
				//console.log(res);
				if (res.data.code == 200) {
					this.$message.success("修改成功")
					this.$router.push('/coursemanager/coursemanagement/courseclass?classid=' + this.$route.query.classid + "&classname=" + this.$route.query.classname)
				}
			})
		},
	},

	mounted() {
		this.getCourses();
		this.getclasssection();
	}
}
</script>

<style lang="less">
.changeclasstable {
	padding: 40px 40px 0px 40px;

	.el-table thead {
		color: #333;
	}

	.navget {
		position: absolute;
		top: 0;
		left: 0;
	}

	.changeclass {
		position: absolute;
		top: 0;
		right: 10%;
		font-size: 15px;
		border-radius: 4px;
	}

	.lookclasstable {
		position: absolute;
		top: 0;
		right: 17%;
		font-size: 15px;
		border-radius: 4px;
	}

	.tip {
		position: absolute;
		top: 30px;
		right: 17%;
		font-size: 10px;
		color: #ccc;
	}

	.courseinput {
		position: absolute;
		width: 15% !important;
		left: 150px;
		top: 0;
	}

	.coursesearchbtn {
		position: absolute;
		left: 380px;
		top: 0;
	}

	.back {
		position: absolute;
		top: 0;
		right: 5%
	}

	.tableFrom {
		padding: 20px;
		background: white;
		text-align: center;
		margin-top: 20px;

		.el-input__inner {
			height: 25px;
			line-height: 25px;
		}

		.el-input__inner:nth-of-type(1) {
			margin-top: 8px
		}

		.el-input__icon {
			line-height: 25px
		}

		.el-table td.el-table__cell div {

			text-align: center;
		}

		.el-table th.el-table__cell>.cell {
			text-align: center;
		}



		.el-table td.el-table__cell,
		.el-table th.el-table__cell.is-leaf {
			border-bottom: 1px solid #333;
		}

		.el-table--border,
		.el-table--group {
			border: 1px solid #333;
		}

		.el-table--border .el-table__cell,
		.el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed {
			border-right: 1px solid #333;
		}

	}

	.el-select.active input {
		border: 1px solid red;
	}
}
</style>